package com.karrim.server.model.entity;

import com.google.common.hash.Hashing;
import com.karrim.server.annotation.Annotation.Portable;
import com.karrim.server.model.abstr.AbstractEntity;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

/**
 *
 * @author andrea.cantafio
 */
@Portable
@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.NEW_TABLE)
public class Account extends AbstractEntity {

    public Account(String email, String password) {
        super();
        this.email = email;
        setPassword(password);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof Account) {
            Account a = (Account) obj;
            return a.email.equals(email) && a.password == password;
        }
        return false;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return String.valueOf(password);
    }

    @Override
    public int hashCode() {
        return Hashing.md5().newHasher().putString(email).putInt(password).hashCode();
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String password) {
        this.password = Hashing.sha512().newHasher().putString(password).hashCode();
    }
    @Persistent
    private String email;
    @Persistent
    private int password;
}
